<template>
  <div>
    <div class="modal-mask"></div>
    <div class="modal-dialog">
      <div class="modal-content">
        <img class="img" src="../../static/images/littleTip-huang.jpg" />
        <div class="content-text">
          <p class="key-bold">真自律是一款自律神器。</p>
          <p class="key-bold">
            将生活想象成通关打怪，打败自己的心魔加分，被心魔打败减分。
          </p>
          <p class="little-tip">举个例子：</p>
          <p class="little-content">
            午饭忍住没有吃麻辣烫，吃的绿色蔬菜，加5分；
          </p>
          <p class="little-content">
            但是晚饭还是没有忍住T_T,罪恶罪恶，减10分。
          </p>
        </div>
      </div>
      <div class="modal-footer">
        <button
          open-type="getUserInfo"
          lang="zh_CN"
          class="btn"
          @getuserinfo="login"
        >
          授权登录
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import config from '@/config'
import qcloud from 'wafer2-client-sdk'
import { showSuccess, post, get } from '@/util'
import userConfig from '@/globalVar'
export default {
  methods: {
    login () {
      qcloud.setLoginUrl(config.loginUrl)
      qcloud.login({
        success: (userInfo) => {
          console.log('登录成功', userInfo)
        },
        fail: (err) => {
          console.log('登录失败', err)
        }
      })

      var that = this
      // 根据code获取openid等信息
      wx.login({
        // 获取code
        success: function (res) {
          var code = res.code // 返回code
          console.info(res)
          console.log('code:' + code)
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data: {
              appid: userConfig.appid, // AppID
              secret: userConfig.secret, // secret密钥
              grant_type: 'authorization_code',
              js_code: code
            },
            header: {
              'content-type': 'json'
            },
            success: function (res) {
              var openid = res.data.openid // 返回openid
              var sessionKey = res.data.session_key // 返回session_key
              console.info(res)
              console.log('openid为' + openid)
              wx.getUserInfo({
                success: function (res) {
                  console.info(res)
                  var userInfo = res.userInfo
                  console.info(userInfo)
                  console.info('获取openid后数据')
                  // 保存openid和session_key
                  userInfo.openId = openid
                  userInfo.skey = sessionKey
                  userInfo.session_key = sessionKey
                  console.info(userInfo)
                  that.loginSuccess(userInfo)
                  // 查询用户
                  // var status = that.selectUser(openid);
                  that.$emit('changeShow', false, userInfo)
                  wx.showTabBar()
                  showSuccess('登录成功')
                },
                fail: function () {
                  console.log('getUserInfo失败')
                }
              })
            },
            fail: function () {
              console.log('获取opid失败')
            }
          })
        },
        fail: function () {
          console.log('登录失败')
        }
      })
    },
    loginSuccess (userInfo) {
      wx.setStorageSync('userinfo', userInfo)
    },
    // 查询用户
    async selectUser (openid) {
      const res = await get('/weapp/getuser', { openid })
      console.info('查询到的用户信息：', res)
      if (res.code === 0) {
        console.info('xh查询的openid', res.open_id)
        // 查询成功
        if (res.open_id === null) {
          console.log('进来了')
          const userinfo = wx.getStorageSync('userinfo')
          const data = {
            openid: userinfo.openid,
            skey: userinfo.session_key,
            user_info: userinfo,
            session_key: userinfo.session_key
          }
          const res = await post('/weapp/createbyxieh', data)
          console.info('自定义添加用户', res)
          console.info('自定义添加用户状态', res.code)
        } else {
          console.log('没进来')
        }
      }
    }
  }
}
</script>

<style lang="scss">
.modal-mask {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: #000;
  opacity: 0.5;
  overflow: hidden;
  z-index: 9000;
  color: #fff;
}
.modal-dialog {
  box-sizing: border-box;
  width: 560rpx;
  overflow: hidden;
  position: fixed;
  top: 30%;
  left: 0;
  z-index: 9999;
  background: #fff;
  margin: -150rpx 95rpx;
  border-radius: 16rpx;
}
.modal-content {
  box-sizing: border-box;
  display: flex;
  padding: 0rpx 53rpx 50rpx 53rpx;
  font-size: 32rpx;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}
.content-tip {
  text-align: center;
  font-size: 36rpx;
  color: #333333;
}
.content-text {
  height: 130px;
  padding: 10px 0px 50px 0px;
  font-size: 14px;
}
.modal-footer {
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  border-top: 1px solid #e5e5e5;
  font-size: 16px;
  font-weight: bold;
  height: 45px;
  line-height: 45px;
  text-align: center;
  background: #feb600;
}
button {
  width: 100%;
  background: #feb600;
  color: #ffffff;
  font-weight: bold;
}
.img {
  width: 280px;
  height: 90px;
}
.little-tip {
  padding-top: 15px;
  padding-bottom: 3px;
  font-size: 14px;
  font-weight: bold;
  color: #feb600;
}
.little-content {
  padding-top: 5px;
  font-size: 13px;
  color: #606060;
}
.key-bold {
  padding-top: 5px;
  font-size: 14px;
  font-weight: bold;
}
</style>